﻿using AbstractMq;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitMqHelp
{
    public class RabbitMQPublisher : IPublisher
    {
        private readonly IConnection _IConnection;
        private readonly IMessageSerializer _IMessageSerializer;
        private readonly ILogger<RabbitMQPublisher> _logger;
        public RabbitMQPublisher(IConnection connection, IMessageSerializer messageSerializer, ILoggerFactory loggerFactory)
        {
            _IConnection = connection;
            _IMessageSerializer = messageSerializer;
            _logger = loggerFactory.CreateLogger<RabbitMQPublisher>();
        }
        public void Publish<T>(string channelName, T message) where T : class
        {
            using (var _Channel = _IConnection.CreateModel())
            {
                _Channel.ExchangeDeclare(exchange: this.GetType().Assembly.GetName().Name, type: "direct", durable: true, autoDelete: false, arguments: null);
                var properties = _Channel.CreateBasicProperties();
                properties.Persistent = true;
                var body = _IMessageSerializer.SerializerBytes(message);
                _Channel.BasicPublish(exchange: this.GetType().Assembly.GetName().Name,
                         routingKey: channelName,
                         basicProperties: properties,
                         body: body);
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug($"发送的信息内容是:exchange:{ this.GetType().Assembly.GetName().Name},type:direct,routingKey:{channelName},body:{body}");
                }
            }
        }

        public void Publish<T>(string exchangeName, string queueName, string routingKey, T messgae) where T : class
        {
            using (var _Channel = _IConnection.CreateModel())
            {
                _Channel.ExchangeDeclare(exchange: exchangeName, type: "fanout", durable: true, autoDelete: false, arguments: null);
                var properties = _Channel.CreateBasicProperties();
                properties.Persistent = true;
                var body = _IMessageSerializer.SerializerBytes(messgae);
                _Channel.BasicPublish(exchange: this.GetType().Assembly.GetName().Name,
                         routingKey: routingKey,
                         basicProperties: properties,
                         body: body);
                if (_logger.IsEnabled(LogLevel.Debug))
                {
                    _logger.LogDebug($"发送的信息内容是:exchange:{exchangeName},type:direct,routingKey:{routingKey},body:{body}");
                }
            }
        }

        public Task PublishAsync<T>(string channelName, T message) where T : class
        {
            throw new NotImplementedException();
        }

        public Task PublishAsync<T>(string exchangeName, string queueName, string routingKey, T messgae) where T : class
        {
            throw new NotImplementedException();
        }
    }
}
